package com.pad.producter.utils;

import java.util.concurrent.atomic.AtomicLong;  

public class UniqueKeyGenerator {  
  
    private static final AtomicLong counter = new AtomicLong(0);  
    private static final long MAX_VALUE = (long) (Math.pow(2, 32) - 1); // 32位整数的最大值  
  
    /**  
     * 生成一个32位长度的自增主键。  
     *  
     * @return 32位长度的自增主键字符串  
     * @throws IllegalStateException 如果计数器超过32位整数的最大值  
     */  
    public static String generateUniqueKey() {  
        long currentValue = counter.incrementAndGet();  
        if (currentValue > MAX_VALUE) {  
            throw new IllegalStateException("The counter has exceeded the maximum value for a 32-bit integer.");  
        }  
        return String.format("%032d", currentValue); // 格式化为32位长度的字符串  
    }  
  
    // 防止实例化  
    private UniqueKeyGenerator() {  
    }  
    
    public static void main(String[] args) {  
        String uniqueKey = UniqueKeyGenerator.generateUniqueKey();  
        System.out.println("Generated Unique Key: " + uniqueKey);  
    }  
}